home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Mac Game Programming Gurus / TricksOfTheMacGameProgrammingGurus.iso / Book Chapters / 05 - User Interaction / KeyConfig w⁄DialogUtils / DialogUtils.c next >
C/C++ Source or Header  |  1995-06-17  |  4KB  |  144 lines

  1. #include "DialogUtils.h"
  2.  
  3.  
  4. /* By Ingemar Ragnemalm 1993-1995 */
  5. /* This file holds some selected calls from my Dialog Utility file,*/
  6. /* translated to C */
  7.  
  8. /* This is yet another unit with small routines for making dialog management
  9. a bit easier. The big difference to all others I've seen is that this one
  10. does some error checking, verifying that whatever it is doing something to
  11. is an item to which you are allowed to do it. This saves *lots* of crashes! */
  12.  
  13.  
  14.  
  15. /*Set the text of a StatText/EditText*/
  16.  
  17. void SetTextDItem(DialogPtr theDialog, short itemNumber, Str255 theString)
  18. {
  19.     short itemKind;
  20.     ControlHandle itemHandle;
  21.     Rect itemBox;
  22.  
  23.     GetDialogItem(theDialog, itemNumber, &itemKind, (Handle *)&itemHandle, &itemBox);
  24.  
  25. /*Check if the itemKind says that it is the right kind of item! IMPORTANT!*/
  26.     itemKind = itemKind & 127;
  27.  
  28.     switch ( itemKind )
  29.     {
  30.         case 8:
  31.         case 16: /*statText, editText*/
  32.             SetDialogItemText((Handle)itemHandle, theString); break;
  33.         case 0:
  34.         case 1:
  35.         case 2:
  36.         case 4:
  37.         case 5:
  38.         case 6: /*button, checkbox, radio - but what is 4?*/
  39.             SetControlTitle(itemHandle, theString); break;
  40.         default: /*Other items have no text to set*/
  41.             SysBeep(1);
  42.     }/*case*/
  43. } /*SetTextDItem*/
  44.  
  45.  
  46. /*Text from StatText/EditText*/
  47. void GetTextDItem(DialogPtr theDialog, short itemNumber, Str255 theString)
  48. {
  49.     short itemKind;
  50.     ControlHandle itemHandle;
  51.     Rect itemBox;
  52.  
  53.     GetDialogItem(theDialog, itemNumber, &itemKind, (Handle *)&itemHandle, &itemBox);
  54.  
  55. /*Check if the itemKind says that it is the right kind of item! IMPORTANT!*/
  56.     itemKind = itemKind & 127;
  57.  
  58.     theString[0] = 0;    /*Set to empty string as default*/
  59.     switch ( itemKind )
  60.     {
  61.         case 8:
  62.         case 16: /*statText, editText*/
  63.             GetDialogItemText((Handle)itemHandle, theString); break;
  64.         case 0:
  65.         case 1:
  66.         case 2:
  67.         case 4:
  68.         case 5:
  69.         case 6: /*button, checkbox, radio - but what is 4?*/
  70.             GetControlTitle(itemHandle, theString); break;
  71.         default: /*Other items have no text to set*/
  72.             SysBeep(1);
  73.     }/*case*/
  74. } /*GetTextDItem*/
  75.  
  76.  
  77. /*Set checkbox/radio*/
  78.  
  79. void SetBooleanDItem(DialogPtr theDialog, short itemNumber, Boolean theBoolean)
  80. {
  81.     short itemKind;
  82.     ControlHandle itemHandle;
  83.     Rect itemBox;
  84.  
  85.     GetDialogItem(theDialog, itemNumber, &itemKind, (Handle *)&itemHandle, &itemBox);
  86.  
  87. /*Check if the itemKind says that it is the right kind of item! IMPORTANT!*/
  88.     itemKind = itemKind & 127;
  89.  
  90.     switch ( itemKind )
  91.     {
  92.         case 8:
  93.         case 16: /*statText, editText*/
  94.             if ( theBoolean )
  95.                 SetDialogItemText((Handle)itemHandle, "\ptrue");
  96.             else
  97.                 SetDialogItemText((Handle)itemHandle, "\pfalse");
  98.             break;
  99.         case 0:
  100.         case 1:
  101.         case 2:
  102.         case 4:
  103.         case 5:
  104.         case 6: /*button, checkbox, radio - but what is 4?*/
  105.             SetControlValue(itemHandle, (short)theBoolean); break;
  106.         default: /*Other items have no text to set*/
  107.             SysBeep(1);
  108.     }/*case*/
  109. } /*SetBooleanDItem*/
  110.  
  111.  
  112. /*Boolean values from checkbox/radio*/
  113.  
  114. Boolean GetBooleanDItem(DialogPtr theDialog, short itemNumber)
  115. {
  116.     short itemKind;
  117.     ControlHandle itemHandle;
  118.     Rect itemBox;
  119.  
  120.     GetDialogItem(theDialog, itemNumber, &itemKind, (Handle *)&itemHandle, &itemBox);
  121.  
  122. /*Check if the itemKind says that it is the right kind of item! IMPORTANT!*/
  123.     itemKind = itemKind & 127;
  124.  
  125.     switch ( itemKind )
  126.     {
  127.         case 0:
  128.         case 1:
  129.         case 2:
  130.         case 4:
  131.         case 5:
  132.         case 6: /*button, checkbox, radio - but what is 4?*/
  133.             return (Boolean)GetControlValue(itemHandle);
  134.         default: /*Others have no value*/
  135.                 SysBeep(1);
  136.     };/*case*/
  137. return false;
  138. } /*GetBooleanDItem*/
  139.  
  140. void ToggleBooleanDItem(DialogPtr theDialog, short itemNumber)
  141. {
  142.     SetBooleanDItem(theDialog, itemNumber, ! GetBooleanDItem(theDialog, itemNumber));
  143. } /*ToggleBooleanDItem*/
  144.